package com.kaizen.codec.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringWriter;

/**
 * TODO 后面升级成Protobuf
 *
 */
@Deprecated
public class JsonUtils {

    private static Logger logger = LoggerFactory.getLogger(JsonUtils.class);


    private static JsonFactory jsonFactory;
    private static ObjectMapper defaultObjectMapper;

    static {
        try {
            jsonFactory = new JsonFactory();
        }catch (Exception e) {
            e.printStackTrace();
        }
        defaultObjectMapper = createObjectMapper();
    }

    public JsonUtils() {
    }

    public static ObjectMapper createObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        objectMapper.registerModule(module);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

        return objectMapper;
    }

    public static <T> String transferToJson(T value) {
        StringWriter sw = new StringWriter();
        JsonGenerator gen = null;

        String res = null;
        try {
            gen = jsonFactory.createGenerator(sw);
            defaultObjectMapper.writeValue(gen, value);
            res = sw.toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }finally {
            if (gen != null) {
                try {
                    gen.close();
                } catch (IOException var11) {
                }
            }

        }

        return res;
    }

    public static <T> T transferToObj(byte[] jsonByte, Class<T> valueType) throws Exception {
        T value = null;
        if (jsonByte != null && jsonByte.length > 0) {
            value = defaultObjectMapper.readValue(jsonByte, valueType);
            return value;
        } else {
            return value;
        }
    }


    public static <T> T transferToObj(String jsonString, Class<T> valueType) throws Exception {
        T value = null;
        if (jsonString != null && jsonString.length() != 0) {
            value = defaultObjectMapper.readValue(jsonString, valueType);
            return value;
        } else {
            return value;
        }
    }

    public static <T> T transferToObj(String jsonString, TypeReference typeReference) throws Exception{
        if (jsonString != null && jsonString.length() != 0 && typeReference != null) {
                return defaultObjectMapper.readValue(jsonString, typeReference);
        } else {
            throw new Exception();
        }
    }


}
